package com.defangit.award.extend.wxmp.config;

import com.defangit.award.extend.wxmp.orm.WxmpUserService;
import com.defangit.openapis.wechat.mp.message.req.RequestMessage;
import com.defangit.openapis.wechat.mp.message.req.TextMessage;
import com.defangit.openapis.wechat.mp.message.req.event.ScanEvent;
import com.defangit.openapis.wechat.mp.message.req.event.SubscibeEvent;
import com.defangit.openapis.wechat.mp.message.req.event.UnSubscribeEvent;
import com.defangit.openapis.wechat.mp.message.resp.ResponseMessage;
import com.defangit.openapis.wechat.mp.message.resp.TextResponse;
import laogen.util.StringUtil;

/**
 * Created by zhangfagen on 2019/1/14.
 */
public class YkWxmpConfig  extends AwardWxmpConfig{
    public YkWxmpConfig(){
        super("yk");
        setAppId("wx7abed5536b7c9c96").setSecret("cc7d54f7d537e7f0dfaa132dcb335f11").setToken("ziiOKRcf7vxbxymoMyPyuGOImkD5nCjX");
    }

    @Override
    public ResponseMessage handleMsg(RequestMessage msg) {

        //new WxmpUserService().addUser(msg.getFromUserName(),getGh(),0);


        ResponseMessage res=null;
        if (msg instanceof TextMessage) {
            res=awardByKey(msg.getFromUserName(),((TextMessage) msg).getContent());
        }

        if (msg instanceof ScanEvent) {//如果用户发送的信息是微信扫码事件
            try {
                String key=((ScanEvent) msg).getEventKey();
                res=awardByKey(msg.getFromUserName(),key);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        if (msg instanceof SubscibeEvent) {
            if(StringUtil.notEmpty(((SubscibeEvent) msg).getEventKey())){
                try {
                    String key=((SubscibeEvent) msg).getEventKey();
                    res=awardByKey(msg.getFromUserName(),key);
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }else{
                res=new TextResponse()
                        .setContent("感谢您关注雅康牙科!");
            }
        }
        if (msg instanceof UnSubscribeEvent) {
            try {

            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        if(res==null){
            res=new TextResponse()
                    .setContent("sorry,没搞懂您想要什么");
        }
        return res;
    }
}
